package leetcode;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;

public class Solution20 {
    ArrayList<Integer> resList;
    int count;
    int maxCount;
    TreeNode pre;
    public int[] findMode(TreeNode root) {
        int count = 0;
        int maxCount = 0;
        TreeNode pre = null;
        resList = new ArrayList<Integer>();
        findMode1(root);
        int[] result = new int[resList.size()];
        for(int i = 0; i < resList.size(); i++){
            result[i] = resList.get(i);
        }
        return result;
    }

    public void findMode1(TreeNode root){
        if(root == null){
            return ;
        }
        //向左遍历
        findMode1(root.left);
        int rootVal = root.val;
        if(pre == null || rootVal != pre.val){
            count = 1;
        }else{
            count++;
        }

        if(count > maxCount){
            resList.clear();
            resList.add(rootVal);
            maxCount = count;
        }else if(count == maxCount){
            resList.add(rootVal);
        }

        pre = root;
        findMode1(root.right);
    }
}
